import copy


def use_list_copy():
    """
    使用list自身的copy
    """
    a = [1, 2, 3]
    b = a.copy()
    b[0] = 10
    print(a)
    print(b)

def use_copy():
    """
    使用copy模块的copy
    """
    c = [1, 2, 3]
    d = copy.copy(c)
    d[0] = 10
    print(id(c))
    print(id(d))
    print(c)
    print(d)


def use_copy2():
    """
    浅copy只做第一层copy
    """
    a = [1, 2]
    b = [3, 4]
    c = [a, b]
    d = copy.copy(c)
    a[0] = 10
    print(id(c))
    print(id(d))
    print(f'c-->{c}')
    print(f'd-->{d}')
    print('-' * 50)
    print(id(c[0]), id(c[1]))
    print(id(d[0]), id(d[1]))


def use_deepcopy():
    """
    深copy会递归地复制所有子对象，新开辟一片空间，再进行赋值
    """
    a = [1, 2]
    b = [3, 4]
    c = [a, b]
    d = copy.deepcopy(c)
    print(id(c))
    print(id(d))
    a[0] = 10
    print(f'c-->{c}')
    print(f'd-->{d}')
    print('-' * 50)
    print(id(c[0]), id(c[1]))
    print(id(d[0]), id(d[1]))


class Hero():
    def __init__(self, name, blood):
        self.name = name
        self.blood = blood
        self.equipment = ['鞋子', '盔甲']

def use_copy_own_obj():
    old_hero = Hero('老王', 100)
    # new_hero = copy.copy(old_hero)
    new_hero = copy.deepcopy(old_hero)
    new_hero.name = '新王'
    new_hero.blood = 80
    new_hero.equipment.append('项链')
    print(old_hero.name, old_hero.blood, old_hero.equipment)
    print(new_hero.name, new_hero.blood, new_hero.equipment)

if __name__ == '__main__':
    # use_copy()
    # use_copy2()
    # use_deepcopy()
    use_copy_own_obj()